package conf2.ui;

import conf2.Configuration;
import conf2.iface.IMenuListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

public class MainMenu extends JMenuBar implements ActionListener {

    private JMenu game,  option,  help;
    private JMenuItem gameAsServer,  gameAsClient,  gameAgainstBot,  gameExit;
    private JMenuItem optionPlaySounds,  optionPopups,  optionSpecialCardReminder,  optionUseHighlight,  helpAbout;
    private IMenuListener menuListener = null;

    public MainMenu(final Configuration config) {

        game = new JMenu("Game");
        game.setMnemonic('G');

        gameAsServer = new JMenuItem("Play as server", 'P');
        gameAsServer.setAccelerator(KeyStroke.getKeyStroke("F2"));
        gameAsServer.addActionListener(this);
        game.add(gameAsServer);

        gameAsClient = new JMenuItem("Play as client", 'c');
        gameAsClient.setAccelerator(KeyStroke.getKeyStroke("F4"));
        gameAsClient.addActionListener(this);
        game.add(gameAsClient);

//        gameAgainstBot = new JMenuItem("Play against bot", 'b');
//        gameAgainstBot.setAccelerator(KeyStroke.getKeyStroke("control B"));
//        gameAgainstBot.addActionListener(this);
//        game.add(gameAgainstBot);

//        game.addSeparator();
//
//        gameGetReport = new JMenuItem("View remaining enemies", 'r');
//        gameGetReport.setAccelerator(KeyStroke.getKeyStroke("control R"));
//        gameGetReport.addActionListener(this);
//        gameGetReport.setEnabled(false);
//        game.add(gameGetReport);

        game.addSeparator();

        gameExit = new JMenuItem("Exit", 'x');
        gameExit.setAccelerator(KeyStroke.getKeyStroke("alt F4"));
        gameExit.addActionListener(this);
        game.add(gameExit);

        add(game);

        option = new JMenu("Option");
        option.setMnemonic('O');

        optionPlaySounds = new JCheckBoxMenuItem("Play sounds");
        optionPlaySounds.setMnemonic('s');
        optionPlaySounds.setAccelerator(KeyStroke.getKeyStroke("control S"));
        optionPlaySounds.addActionListener(this);
        optionPlaySounds.setSelected(config.getSound());
        option.add(optionPlaySounds);

        optionPopups = new JCheckBoxMenuItem("\"Your turn\" popup alerts");
        optionPopups.setMnemonic('Y');
        optionPopups.setAccelerator(KeyStroke.getKeyStroke("control P"));
        optionPopups.addActionListener(this);
        optionPopups.setSelected(config.getPopup());
        option.add(optionPopups);

        option.addSeparator();

        optionSpecialCardReminder = new JCheckBoxMenuItem("Confirm when playing special cards");
        optionSpecialCardReminder.setMnemonic('A');
        optionSpecialCardReminder.setAccelerator(KeyStroke.getKeyStroke("control L"));
        optionSpecialCardReminder.addActionListener(this);
        optionSpecialCardReminder.setSelected(config.getSpecialCardReminder());
        option.add(optionSpecialCardReminder);

        optionUseHighlight = new JCheckBoxMenuItem("Use movement assist highlighting");
        optionUseHighlight.setMnemonic('h');
        optionUseHighlight.setAccelerator(KeyStroke.getKeyStroke("control H"));
        optionUseHighlight.addActionListener(this);
        optionUseHighlight.setSelected(config.getMovementAssist());
        option.add(optionUseHighlight);

        add(option);

        help = new JMenu("Help");
        help.setMnemonic('H');

        helpAbout = new JMenuItem("About", 'b');
        helpAbout.setAccelerator(KeyStroke.getKeyStroke("F1"));
        helpAbout.addActionListener(this);
        help.add(helpAbout);

        add(help);
    }

    public void setGameAsServerEnabled(boolean enabled) {
        gameAsServer.setEnabled(enabled);
    }

    public void setGameAsClientEnabled(boolean enabled) {
        gameAsClient.setEnabled(enabled);
    }

    public void setMenuListener(IMenuListener l) {
        menuListener = l;
    }

    public void actionPerformed(ActionEvent e) {
        if (menuListener == null) {
            return;
        }

        JMenuItem item = (JMenuItem) e.getSource();
        if (item == gameExit) {
            menuListener.gameExit();
        } else if (item == helpAbout) {
            menuListener.helpAbout();
        } else if (item == gameAsServer) {
            menuListener.gameAsServer();
        } else if (item == gameAsClient) {
            menuListener.gameAsClient();
        } else if (item == gameAgainstBot) {
//            menuListener.gameAgainstBot(game);
        } else if (item == optionPlaySounds) {
            menuListener.sound(optionPlaySounds.isSelected());
        } else if (item == optionPopups) {
            menuListener.popup(optionPopups.isSelected());
        } else if (item == optionSpecialCardReminder) {
            menuListener.specialCardReminder(optionSpecialCardReminder.isSelected());
        } else if (item == optionUseHighlight) {
            menuListener.useHighlight(optionUseHighlight.isSelected());
        }
    }
}
